package com.fingard.dsp.bank.directbank.mybank01;

import java.io.File;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.Date;

import com.fingard.FGBiz;
import com.fingard.constant.Format;
import com.fingard.dsp.bank.reqrespobj.batpayrec.ReqBatPayRec;
import com.fingard.dsp.bankset.BankFront;
import com.fingard.io.FileHelper;
import com.fingard.text.StringHelper;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

/**
 * Created by Ross on 2017-06-23.
 */
public class NoticeBank extends MYBank01Base {

    private ReqBatPayRec p_req1988;
    private String fileName;
    private static final String MONEY_FORMAT = "000000000000000";

    public NoticeBank(ReqBatPayRec reqBatPayRec, BankFront p_BankFront, String p_FileName) {
        this.p_req1988 = reqBatPayRec;
        this.fileName = p_FileName;
        this.reqHeader = reqBatPayRec.reqHeader;
        this.bankFront = p_BankFront;
        this.setSbLog(new StringBuilder());
    }

    public void send() {
//新定义线程来处理发送
        new Thread() {
            public void run() {
                synchronized (this) {
                    Date startTime = new Date();
                    try {
                        Thread.currentThread().wait(180000);
                        System.out.println("开始进行批量转账确认");
                        WriteBankLogLn2("开始进行批量转账确认");
                        MYBank01Header bankHeader = new MYBank01Header();
                        bankHeader.function = "ant.ebank.transfer.batchtransfer.confirm";
                        bankHeader.signFields = "bizNo,smsCode,fileName,totalCount,totalAmount,currencyCode,remark,companyCardNo";
                        bankHeader.signValueField = "sign";

                        Date tmpDateNow = new Date();
                        bankHeader.reqMsgID = Format.DateTime17Format.format(tmpDateNow);
                        bankHeader.reqTime = tmpDateNow;

                        XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
                        writeCommonHeader(xmlWriter, bankHeader);
                        xmlWriter.writeStartElement("body");
                        xmlWriter.writeElementString("partner", bankFront.operator);
                        String batchNo = String.format("%032d", Long.parseLong(reqHeader.transSeqID));
                        xmlWriter.writeElementString("bizNo", batchNo);
                        xmlWriter.writeElementString("smsCode", p_req1988.smsCode);
                        xmlWriter.writeElementString("fileName", fileName);
                        xmlWriter.writeElementString("totalCount", p_req1988.totalNum);
                        BigDecimal curUnit = new BigDecimal(100);
                        BigDecimal amt = new BigDecimal(p_req1988.totalAmt);
                        amt = amt.multiply(curUnit).setScale(0, BigDecimal.ROUND_HALF_UP);
                        DecimalFormat df = new DecimalFormat(MONEY_FORMAT);
                        xmlWriter.writeElementString("totalAmount", df.format(amt));
                        xmlWriter.writeElementString("currencyCode", "156");
                        String remark = StringHelper.isNullOrEmpty(p_req1988.useType) ? "转账" : p_req1988.useType;
                        xmlWriter.writeElementString("remark", remark);
                        xmlWriter.writeElementString("companyCardNo", p_req1988.ownAct);
                        xmlWriter.writeElementString("sign", "");
                        xmlWriter.writeEndElement();
                        xmlWriter.writeEndDocument();

                        String[] tmpStrRet = sendToBank(xmlWriter.toXmlString(), bankHeader);
                        String dataDir = getDataFileDirectory(p_req1988.getPayDateTime());
                        File tmpFile = new File(dataDir, p_req1988.reqBatchNO);
                        String fileContent = "";
                        if (tmpStrRet[0].length() == 0) {
                            StringBuilder tmpSbMsg = new StringBuilder();
                            XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                            //System.out.println("银行返回报文:"+tmpStrRet[1]);
                            if (tmpBkReader != null) {
                                String tmpRetCode = tmpBkReader.getFirstText("/document/response/body/resultInfo/resultStatus");
                                String tmpRetInfo = tmpBkReader.getFirstText("/document/response/body/resultInfo/resultMsg");
                                String resultCode=tmpBkReader.getFirstText("/document/response/body/resultInfo/resultCode");
                                fileContent += tmpRetCode +"+"+resultCode+ ":" + tmpRetInfo + "|";
                                if (tmpRetCode != null && tmpRetCode.equals("S")) {
                                    if (tmpBkReader.read("/document/response/body")) {
                                        String batchTransNo = tmpBkReader.readedXElement.getSingleText("batchTransNo");
                                        fileContent += p_req1988.reqBatchNO + ":" + batchTransNo;
                                        WriteBankLogLn2("生成银行返回流水号文件成功");
                                    }
                                }
                            }
                        }
                        else{
                            fileContent += "F:" + tmpStrRet[1] + "|";
                        }
                        FileHelper.writeFile(tmpFile, fileContent, getCharset());
                        String tmpDateDir = Format.DateForLogFormat.format(startTime).replace("-", File.separator);
                        String tmpSubFileName = "BankLogs" + File.separator + tmpDateDir + File.separator + reqHeader.bankCode + File.separator + reqHeader.transType + "_" + Format.DateTime14Format.format(startTime) + "_" + p_req1988.reqBatchNO + "_notice.txt";
                        String tmpLogFile = FGBiz.dspCenter.bankLogsDirPath + File.separator + tmpSubFileName;
                        FileHelper.writeFile(tmpLogFile, getSbLog());

                    } catch (InterruptedException ex) {
                        ex.printStackTrace();
                    } catch (Exception ex1) {
                        ex1.printStackTrace();
                    }
                }
            }
        }.start();
    }


}
